package com.defysoft.waukids;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.intro.Intro_Join_A;
import com.defysoft.waukids.intro.Intro_Login_A;


public class Intro_Yacguan extends BaseActivity {
	
	Toolbar toolbar;
	private final int FAIL = 9;
	private final int SUCCESS= 10;
	String[] yacguanlist = new String[6];
	Button button;
	String uuid="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro__yacguan);
		setStartData();
	}
	@Override
	public void setStartData() {
		// TODO Auto-generated method stub
		super.setStartData();
		uuid = getIntent().getStringExtra("INTENT_EXTRA_INTRO_UUID");
		if (uuid == null) {
			uuid = "";
		}
		button =(Button)findViewById(R.id.YACGUAN_BTN);
		toolbar = (Toolbar) findViewById(R.id.YACGUAN_TOOL);
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(Intro_Yacguan.this, "약관에 동의하셔야 회원가입이 가능합니다.", Toast.LENGTH_SHORT).show();
				Intent  i = new Intent();
				i.putExtra("yacguan", "no");
				setResult(INTRO_YACGUAN_TO_JOIN_OK, i);
				finish();
			}
		});
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent  i = new Intent();
				i.putExtra("yacguan", "ok");
				setResult(INTRO_YACGUAN_TO_JOIN_OK, i);
				/*startActivityForResult(new Intent(Intro_Yacguan.this, Intro_Join_A.class).putExtra(INTENT_EXTRA_INTRO_UUID, uuid), INTRO_JOIN_TO_LOGIN);*/
				/*startActivity(new Intent(Intro_Yacguan.this, Intro_Join_A.class).putExtra(INTENT_EXTRA_INTRO_UUID, uuid));*/
				finish();
			}
		});
		startNetwork(1);
	}
	
	@Override
	public void startThread(int choice) {
		// TODO Auto-generated method stub
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case 1:
					mainThread();
					break;
			
				}
			}
		});
		firstThread.start();
		startDIal();
	}
	
	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
		switch (_message.what) {
		case CATCH_EXCEPTION: // thread 에러 메세지
			if (firstThread != null) {firstThread.interrupt();}
			endDIal();
			Toast.makeText(Intro_Yacguan.this, (String) _message.obj, Toast.LENGTH_LONG).show();
			break;
		case FAIL:	
			if (firstThread != null) {firstThread.interrupt();}
			endDIal();
			Toast.makeText(Intro_Yacguan.this, "통신실패", Toast.LENGTH_LONG).show();
			break;
		case SUCCESS:	
			if (firstThread != null) {firstThread.interrupt();}
			endDIal();
			((TextView)findViewById(R.id.YACGUAN_FIRST_NAME)).setText(yacguanlist[0]);
			((TextView)findViewById(R.id.YACGUAN_FIRST_SUB)).setText(yacguanlist[1]);
			((TextView)findViewById(R.id.YACGUAN_TWO_NAME)).setText(yacguanlist[2]);
			((TextView)findViewById(R.id.YACGUAN_TWO_SUB)).setText(yacguanlist[3]);
			((TextView)findViewById(R.id.YACGUAN_THREE_NAME)).setText(yacguanlist[4]);
			((TextView)findViewById(R.id.YACGUAN_THREE_SUB)).setText(yacguanlist[5]);
			break;
		
		}
	}
	
	private void mainThread() {
		// TODO Auto-generated method stub
		try {
			String set = publicHttpCallback(HTTP_YACGUAN, "");
			JSONObject jsonObject = new JSONObject(set);
			if (jsonObject.getBoolean("success")) {
				JSONObject ja=new JSONObject(jsonObject.getString("payload"));
				yacguanlist[0]=ja.getString("First_Name");
				yacguanlist[1]=ja.getString("First_Sub");
				yacguanlist[2]=ja.getString("Two_Name");
				yacguanlist[3]=ja.getString("Two_Sub");
				yacguanlist[4]=ja.getString("Three_Name");
				yacguanlist[5]=ja.getString("Three_Sub");
				onNetworkCallback(SUCCESS, "");
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, "main" + e.toString());
		}
		;
	}
	//뒤로 두번 눌렀을때
	private boolean mPressFirstBackKey = false;
	private Timer timer;
	@Override
	public void onBackPressed() {
		if (mPressFirstBackKey == false) {
			Toast.makeText(this, "약관에 동의하셔야 회원가입이 가능합니다.\n버튼을 한번 더 누르시면 로그인페이지로 이동합니다.", Toast.LENGTH_SHORT).show();
			mPressFirstBackKey = true;
			TimerTask second = new TimerTask() {
				@Override
				public void run() {
					timer.cancel();
					timer = null;
					mPressFirstBackKey = false;
				}
			};
			if (timer != null) {
				timer.cancel();
				timer = null;
			}
			timer = new Timer();
			timer.schedule(second, 2000);
		} else {
			Intent  i = new Intent();
			i.putExtra("yacguan", "no");
			setResult(INTRO_YACGUAN_TO_JOIN_OK, i);
			super.onBackPressed();
		}

	}
}
